package work.array;

import java.util.Arrays;

/**
 * @author WDH
 * @description
 * @date 2024年12月04日 21:22
 */

public class Problem977 {

    public int[] sortedSquares(int[] nums) {
        for (int i = 0; i < nums.length; i++) {
            nums[i] = nums[i] * nums[i];
        }
        Arrays.sort(nums);
        return nums;
    }

    public int[] sortedSquares2(int[] nums) {
        int first = 0, tail = nums.length - 1;
        int[] ans = new int[nums.length];
        int x = tail;
        while (first <= tail) {
            int i = nums[first] * nums[first];
            int j = nums[tail] * nums[tail];
            if (i > j) {
                ans[x--] = i;
                first++;
            } else {
                ans[x--] = j;
                tail--;
            }
        }
        return ans;
    }
}
